﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using KZ400B;
using System.Configuration;
using KZ400B.Controls;
using KZ400B.Data;
using System.Drawing;
using System.Windows.Interop;
namespace KZ400BApp
{
    public class AxleT_PssType: PropertyChangedBase
    {
        public AxleT_PssType()
        {
            m_IsWhirling = false;
            m_Unit = RunningUnits.UnDefined;
            m_CurrentAngle = 0;
        }

        bool m_IsWhirling;
        public bool IsWhirling
        {
            get { return m_IsWhirling; }
            private set 
            { 
                if (m_IsWhirling != value) 
                { 
                    m_IsWhirling = value; 
                    this.NotifyPropertyChanged(n=>IsWhirling); 
                } 
            }
        }

        RunningUnits m_Unit;
        public RunningUnits Unit
        {
            get { return m_Unit; }
            set 
            { 
                m_Unit = value;
                IsWhirling = m_Unit == RunningUnits.Radian;
            }
        }

        int m_CurrentAngle;

        public int CurrentAngle
        {
            get { return m_CurrentAngle; }
            set { m_CurrentAngle = value; }
        }

    }

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<SPort> m_SPortList = new List<SPort>();//getlist() //from database
        IconManage m_IconManager;
        AxleT_PssType m_ATStatus;
        void initSettings()
        {
            MySetting.PositionDisplayDecimals = Properties.Settings.Default.PositionDisplayDecimals;
            MySetting.OSDisplayDecimals = Properties.Settings.Default.OSDisplayDecimals;
            MySetting.RunPWMLimit = Properties.Settings.Default.RunPWMLimit;
            MySetting.ToZeroPWMLimit = Properties.Settings.Default.ToZeroPWMLimit;
            MySetting.PWMRatio = Properties.Settings.Default.PWMRatio;


            MySetting.setUnionStoreAxleValue("X", Properties.Settings.Default.X_UnionStore);
            MySetting.setUnionStoreAxleValue("Y", Properties.Settings.Default.Y_UnionStore);
            MySetting.setUnionStoreAxleValue("Z", Properties.Settings.Default.Z_UnionStore);
            MySetting.setUnionStoreAxleValue("T", Properties.Settings.Default.T_UnionStore);
            MySetting.setUnionStoreAxleValue("U", Properties.Settings.Default.U_UnionStore);
            MySetting.setUnionStoreAxleValue("W", Properties.Settings.Default.W_UnionStore);


            MySetting.setVZeroSetStoreAxleValue("X", Properties.Settings.Default.X_VZeroIsSet);
            MySetting.setVZeroSetStoreAxleValue("Y", Properties.Settings.Default.Y_VZeroIsSet);
            MySetting.setVZeroSetStoreAxleValue("Z", Properties.Settings.Default.Z_VZeroIsSet);
            MySetting.setVZeroSetStoreAxleValue("T", Properties.Settings.Default.T_VZeroIsSet);
            MySetting.setVZeroSetStoreAxleValue("U", Properties.Settings.Default.U_VZeroIsSet);
            MySetting.setVZeroSetStoreAxleValue("W", Properties.Settings.Default.W_VZeroIsSet);


            MySetting.OnUserSettingChange += new UserSettingChangeHandle(MySetting_OnUserSettingChange);


#if DEBUG
            btnSimulate.Visibility = System.Windows.Visibility.Visible;
            spState.Visibility = System.Windows.Visibility.Visible;
#else
            btnSimulate.Visibility = System.Windows.Visibility.Collapsed;
            spState.Visibility = System.Windows.Visibility.Collapsed;
#endif
        }

        void MySetting_OnUserSettingChange()
        {
            Properties.Settings.Default.PositionDisplayDecimals = MySetting.PositionDisplayDecimals;
            Properties.Settings.Default.OSDisplayDecimals = MySetting.OSDisplayDecimals;
            Properties.Settings.Default.RunPWMLimit = MySetting.RunPWMLimit;
            Properties.Settings.Default.ToZeroPWMLimit = MySetting.ToZeroPWMLimit;
            Properties.Settings.Default.PWMRatio = MySetting.PWMRatio;

            Properties.Settings.Default.X_UnionStore = MySetting.getUnionStoreAxleValue("X");
            Properties.Settings.Default.Y_UnionStore = MySetting.getUnionStoreAxleValue("Y");
            Properties.Settings.Default.Z_UnionStore = MySetting.getUnionStoreAxleValue("Z");
            Properties.Settings.Default.T_UnionStore = MySetting.getUnionStoreAxleValue("T");
            Properties.Settings.Default.U_UnionStore = MySetting.getUnionStoreAxleValue("U");
            Properties.Settings.Default.W_UnionStore = MySetting.getUnionStoreAxleValue("W");




            Properties.Settings.Default.X_VZeroIsSet = MySetting.getVZeroSetStoreAxleValue("X");
            Properties.Settings.Default.Y_VZeroIsSet = MySetting.getVZeroSetStoreAxleValue("Y");
            Properties.Settings.Default.Z_VZeroIsSet = MySetting.getVZeroSetStoreAxleValue("Z");
            Properties.Settings.Default.T_VZeroIsSet = MySetting.getVZeroSetStoreAxleValue("T");
            Properties.Settings.Default.U_VZeroIsSet = MySetting.getVZeroSetStoreAxleValue("U");
            Properties.Settings.Default.W_VZeroIsSet = MySetting.getVZeroSetStoreAxleValue("W");


            Properties.Settings.Default.Save();
            asX.RefreshDisplay();
            asY.RefreshDisplay();
            asZ.RefreshDisplay();
            asT.RefreshDisplay();
            asU.RefreshDisplay();
            asW.RefreshDisplay();


        }

        public MainWindow()
        {
            InitializeComponent();
            m_ATStatus = new AxleT_PssType();
            m_IconManager = IconManage.getInstance();

            m_IconManager.Connected = ChangeBitmapToBitmapSource(Properties.Resources.connected);
            m_IconManager.DisConnected = ChangeBitmapToBitmapSource(Properties.Resources.disconnected);
            m_IconManager.MultiAxles = ChangeBitmapToBitmapSource(Properties.Resources.multiAxles);
            m_IconManager.UserControl = ChangeBitmapToBitmapSource(Properties.Resources.userControl);
            m_IconManager.Config2 = ChangeBitmapToBitmapSource(Properties.Resources.config2);

            spMenus.DataContext = m_IconManager;

            

            AxleCacheManager.InitFromDB();
            RunningUnitCacheManager.InitFromDB();
            Dictionary<string, Axle> axDict = AxleCacheManager.AxleList;
            foreach (KeyValuePair<string, Axle> ax in axDict)
            {
                PositionCacheManager.ChangePosition(ax.Key, ax.Value.Position);
            }

            

#if DEBUG

#endif

            initSettings();
            foreach (string aName in AxleCacheManager.AxleList.Keys)
            {
                AxleCacheManager.AxleList[aName].AxleActionStatus.VZeroIsSeted = MySetting.getVZeroSetStoreAxleValue(aName);
            }

            this.DataContext = PositionCacheManager.AxleStates;
            bindSPort();
            AxleManager.OnAxleActionResponse += new AxleActionResponse(AxleManager_OnAxleActionResponse);
            //PositionCacheManager.OnAxlePostionChange += new AxlePositionChange(AxleManager_OnAxlePostionChange);

            AxleRunState.Instance.Regist(AxleCacheManager.AxleList, AxleActions.AllActions);
            StateMsgManager.Start();
            OneStepAddManager.Start();
            labState.DataContext = AxleStateBarMsg.Instance;


            //asX.OnStateChanged += new KZ400B.Controls.StateChange(asX_OnStateChanged);
            //asY.OnStateChanged += new KZ400B.Controls.StateChange(asY_OnStateChanged);
            //asZ.OnStateChanged += new KZ400B.Controls.StateChange(asZ_OnStateChanged);
            //asT.OnStateChanged += new KZ400B.Controls.StateChange(asT_OnStateChanged);
            //asU.OnStateChanged += new KZ400B.Controls.StateChange(asU_OnStateChanged);
            //asW.OnStateChanged += new KZ400B.Controls.StateChange(asW_OnStateChanged);

            //asX.OnOneStepStateChange += new KZ400B.Controls.OneStepStateChange(asX_OnOneStepStateChange);
            //asY.OnOneStepStateChange += new KZ400B.Controls.OneStepStateChange(asY_OnOneStepStateChange);
            //asZ.OnOneStepStateChange += new KZ400B.Controls.OneStepStateChange(asZ_OnOneStepStateChange);
            //asT.OnOneStepStateChange += new KZ400B.Controls.OneStepStateChange(asT_OnOneStepStateChange);
            //asU.OnOneStepStateChange += new KZ400B.Controls.OneStepStateChange(asU_OnOneStepStateChange);
            //asW.OnOneStepStateChange += new KZ400B.Controls.OneStepStateChange(asW_OnOneStepStateChange);


            Xsetting.OnUnitChanged += new KZ400B.Controls.UnitChanged(Xsetting_OnUnitChanged);
            Ysetting.OnUnitChanged += new KZ400B.Controls.UnitChanged(Ysetting_OnUnitChanged);
            Zsetting.OnUnitChanged += new KZ400B.Controls.UnitChanged(Zsetting_OnUnitChanged);
            Tsetting.OnUnitChanged += new KZ400B.Controls.UnitChanged(Tsetting_OnUnitChanged);
            Usetting.OnUnitChanged += new KZ400B.Controls.UnitChanged(Usetting_OnUnitChanged);
            Wsetting.OnUnitChanged += new KZ400B.Controls.UnitChanged(Wsetting_OnUnitChanged);

            spAxleTAngle.DataContext = m_ATStatus;
            asT.Loaded += asT_Loaded;
        }

        void asT_Loaded(object sender, RoutedEventArgs e)
        {
            m_ATStatus.Unit = (RunningUnits)asT.AxlePositionUnit;
        }



        void asX_OnOneStepStateChange(string aName, bool bRun)
        {

        }

        void asY_OnOneStepStateChange(string aName, bool bRun)
        {

        }

        void asZ_OnOneStepStateChange(string aName, bool bRun)
        {

        }

        void asT_OnOneStepStateChange(string aName, bool bRun)
        {

        }

        void asU_OnOneStepStateChange(string aName, bool bRun)
        {

        }

        void asW_OnOneStepStateChange(string aName, bool bRun)
        {

        }

        #region 单位变化事件
        void Xsetting_OnUnitChanged(string axleName, int newUnit)
        {
            if (!string.IsNullOrEmpty(axleName)
                //&& !string.IsNullOrEmpty(newUnit)
                && asX.AxleName.Equals(axleName)
                )
            {
                asX.AxlePositionUnit = newUnit;
            }
        }
        void Ysetting_OnUnitChanged(string axleName, int newUnit)
        {
            if (!string.IsNullOrEmpty(axleName)
                //&& !string.IsNullOrEmpty(newUnit)
                && asY.AxleName.Equals(axleName)
                )
            {
                asY.AxlePositionUnit = newUnit;
            }

        }
        void Zsetting_OnUnitChanged(string axleName, int newUnit)
        {
            if (!string.IsNullOrEmpty(axleName)
                //&& !string.IsNullOrEmpty(newUnit)
                && asZ.AxleName.Equals(axleName)
                )
            {
                asZ.AxlePositionUnit = newUnit;
            }
        }
        void Tsetting_OnUnitChanged(string axleName, int newUnit)
        {
            if (!string.IsNullOrEmpty(axleName)
                //&& !string.IsNullOrEmpty(newUnit)
                && asT.AxleName.Equals(axleName)
                )
            {
                asT.AxlePositionUnit = newUnit;
                m_ATStatus.Unit = (RunningUnits)newUnit;
            }
        }
        void Usetting_OnUnitChanged(string axleName, int newUnit)
        {
            if (!string.IsNullOrEmpty(axleName)
                //&& !string.IsNullOrEmpty(newUnit)
                && asU.AxleName.Equals(axleName)
                )
            {
                asU.AxlePositionUnit = newUnit;
            }
        }
        void Wsetting_OnUnitChanged(string axleName, int newUnit)
        {
            if (!string.IsNullOrEmpty(axleName)
                //&& !string.IsNullOrEmpty(newUnit)
                && asW.AxleName.Equals(axleName)
                )
            {
                asW.AxlePositionUnit = newUnit;
            }
        }
        #endregion

        #region 运行状态改变
        void asX_OnStateChanged(AxleAction action, string state)
        {
            invokeStateChange(state);
        }
        void asY_OnStateChanged(AxleAction action, string state)
        {
            invokeStateChange(state);
        }
        void asZ_OnStateChanged(AxleAction action, string state)
        {
            invokeStateChange(state);
        }
        void asT_OnStateChanged(AxleAction action, string state)
        {
            invokeStateChange(state);
        }
        void asU_OnStateChanged(AxleAction action, string state)
        {
            invokeStateChange(state);
        }
        void asW_OnStateChanged(AxleAction action, string state)
        {
            invokeStateChange(state);
        }

        void invokeButtonChange(string axleName, AxleActions action)
        {
            //if (action == AxleActions.RunFinish)
            //    { 
            //        if(axleName.Equals("X"))
            //        {

            //        }
            //    }
            //    else if(action == AxleActions.StopFinish)
            //    {

            //    }
        }

        void AxleManager_OnAxleActionResponse(
            string axleName
            , Axle axle
            , AxleActions action
            , bool success
            , string message
        )
        {
            invokeStateChange(message);
            invokeButtonChange(axleName, action);
            //if (action == AxleActions.ToZeroFinish)
            //{
            //    ParameterCacheManager.Update(axleName, Axle.Direction.ForwardDirection, 0.0F, 0.0F);
            //}
            //if (
            //    action == AxleActions.RunFinish
            //    || action == AxleActions.StopFinish
            //    )
            //{
            //    if (success)
            //    {
            //        AxleManager.QueryPosition(axleName);
            //    }
            //}
            if (action == AxleActions.ToZeroFinish
            || action == AxleActions.RunFinish
            || action == AxleActions.StopFinish
            )
            {
                switch (axleName)
                {
                    //case "X": asX.setBtnState_AxleNotRunning(); break;
                    //case "Y": asY.setBtnState_AxleNotRunning(); break;
                    //case "Z": asZ.setBtnState_AxleNotRunning(); break;
                    //case "T": asT.setBtnState_AxleNotRunning(); break;
                    //case "U": asU.setBtnState_AxleNotRunning(); break;
                    //case "W": asW.setBtnState_AxleNotRunning(); break;
                    ////case "X": asX.setBtnState_AxleRunning(); break;
                    ////case "Y": asY.setBtnState_AxleRunning(); break;
                    ////case "Z": asZ.setBtnState_AxleRunning(); break;
                    ////case "T": asT.setBtnState_AxleRunning(); break;
                    ////case "U": asU.setBtnState_AxleRunning(); break;
                    ////case "W": asW.setBtnState_AxleRunning(); break;
                    default: break;
                }
            }

            //if (success && action == AxleActions.RunFinish)
            //{
            //    switch (axleName)
            //    {
            //        case "X": if (asX.IsOneStepRunning) { asX.AddOneStepAddtion(); }; break;
            //        case "Y": if (asY.IsOneStepRunning) { asY.AddOneStepAddtion(); }; break;
            //        case "Z": if (asZ.IsOneStepRunning) { asZ.AddOneStepAddtion(); }; break;
            //        case "T": if (asT.IsOneStepRunning) { asT.AddOneStepAddtion(); }; break;
            //        case "U": if (asU.IsOneStepRunning) { asU.AddOneStepAddtion(); }; break;
            //        case "W": if (asW.IsOneStepRunning) { asW.AddOneStepAddtion(); }; break;
            //        default: break;
            //    }
            //}


        }

        void invokeStateChange(string state)
        {
            //if (labState != null && !string.IsNullOrEmpty(state))
            //{
            //    labState.Dispatcher.Invoke(new Action(delegate()
            //    {
            //        labState.Content = state;
            //    }
            //    ));
            //}
            MsgStateItem itm = new MsgStateItem(labState, state);
            StateMsgManager.addOneState(itm);
        }


        #endregion
        void invokePositionChange(KZ400B.Controls.nAxleStateV2 control, string axleName, double position)
        {
            if (control != null && !string.IsNullOrEmpty(axleName))
            {
                control.Dispatcher.Invoke(new Action(delegate()
                {
                    PositionCacheManager.ChangePosition(axleName, position, true);
                }
                ));
            }
        }

        void invokePositionChange(KZ400B.Controls.nAxleState control, string axleName, double position)
        {
            if (control != null && !string.IsNullOrEmpty(axleName))
            {
                control.Dispatcher.Invoke(new Action(delegate()
                {
                    PositionCacheManager.ChangePosition(axleName, position, true);
                }
                ));
            }
        }
        void AxleManager_OnAxlePostionChange(string axleName, Axle axle, double position, bool ack)
        {
            if (!ack)
            {
                invokeStateChange("位置查询返回信息不正确!");
                return;
            }

            if (axleName.Equals("X"))
            {
                //asX.AxlePosition = position;
                invokePositionChange(asX, axleName, position);
            }
            else if (axleName.Equals("Y"))
            {
                //asY.AxlePosition = position;
                invokePositionChange(asY, axleName, position);
            }
            else if (axleName.Equals("Z"))
            {
                //asZ.AxlePosition = position;
                invokePositionChange(asZ, axleName, position);
            }
            else if (axleName.Equals("T"))
            {
                //asT.AxlePosition = position;
                invokePositionChange(asT, axleName, position);
            }
            else if (axleName.Equals("U"))
            {
                //asU.AxlePosition = position;
                invokePositionChange(asU, axleName, position);
            }
            else if (axleName.Equals("W"))
            {
                //asW.AxlePosition = position;
                invokePositionChange(asW, axleName, position);
            }
        }




        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Xsetting.Save();
            Ysetting.Save();
            Zsetting.Save();
            Tsetting.Save();
            Usetting.Save();
            Wsetting.Save();

            asX.AxlePositionUnit = Xsetting.RunningUnit;
            asY.AxlePositionUnit = Ysetting.RunningUnit;
            asZ.AxlePositionUnit = Zsetting.RunningUnit;
            asT.AxlePositionUnit = Tsetting.RunningUnit;
            asU.AxlePositionUnit = Usetting.RunningUnit;
            asW.AxlePositionUnit = Wsetting.RunningUnit;

            //调用参数保留命令
            AxleManager.ParameterPreserve();

        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            //Xsetting.Refresh();
            //Ysetting.Refresh();
            //Zsetting.Refresh();
            //Tsetting.Refresh();
            //Usetting.Refresh();
            //Wsetting.Refresh();

            Xsetting.Reload();
            Ysetting.Reload();
            Zsetting.Reload();
            Tsetting.Reload();
            Usetting.Reload();
            Wsetting.Reload();
        }

        void bindSPort()
        {
            List<int> m_List = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 ,10
                                    ,11,12,13,14,15,16,17,18,19,20
            };
            List<string> m_sList = new List<string>();
            foreach (int iPort in m_List)
            {
                m_sList.Add(string.Format("COM{0}", iPort));
            }
            cmbSPortList.ItemsSource = m_sList;
            cmbSPortList.SelectedIndex = 0;
            string pName = string.Empty;
            try
            {
                pName = Properties.Settings.Default.PortName;
                if (!string.IsNullOrEmpty(pName))
                {
                    cmbSPortList.SelectedItem = pName;
                }
            }
            catch (Exception ex)
            {
                Utility.FileOperate.WriteToLog(string.Format("设置默认端口[{0}]失败:{1}", pName, ex.Message));
            }

        }

        private void btnMultiAxle_Click(object sender, RoutedEventArgs e)
        {
            MultiAxleControl mac = new MultiAxleControl();
            mac.ShowDialog();
        }

        /// <summary>

        /// 从Bitmap转换成BitmapSource

        /// </summary>

        /// <param name="bmp"></param>

        /// <returns></returns>

        public static BitmapSource ChangeBitmapToBitmapSource(Bitmap bmp)
        {

            BitmapSource returnSource;

            try
            {
                returnSource = Imaging.CreateBitmapSourceFromHBitmap
                    (
                         bmp.GetHbitmap(), IntPtr.Zero,
                         Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()
                    );
            }

            catch
            {
                returnSource = null;
            }


            return returnSource;


        }

        bool m_isConnected = false;
        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            btnConnect.IsEnabled = false;
            WpfEvent.DoEvents();
            if (!m_isConnected)
            {

                //todo:get from m_SPortList
                SPort sport = new SPort();
                sport.PortName = string.Format("{0}", cmbSPortList.SelectedItem.ToString());
                if (AxleManager.Open(sport))
                {
                    m_isConnected = true;
                    imgConnection.Source = m_IconManager.Connected;
                    //ChangeBitmapToBitmapSource(Properties.Resources.connected);
                    //    new BitmapImage(
                    //new Uri(
                    //    "/KZ600BApp;component/Images/connected.jpg"
                    //    , UriKind.Relative
                    //    )
                    //);
                    tbCxTip.Text = "断开";
                    try
                    {
                        Properties.Settings.Default.PortName = cmbSPortList.SelectedItem.ToString();
                        Properties.Settings.Default.Save();
                    }
                    catch (Exception ex)
                    {
                        Utility.FileOperate.WriteToLog(
                            string.Format("save setting [PortName] = [{0}] Error:{1}", cmbSPortList.SelectedItem.ToString()
                            , ex.Message
                            ));
                    }
                }
                else
                {
                    btnConnect.IsEnabled = true;
                    MessageBox.Show(string.Format("端口{0}打开失败", sport.PortName));
                }
            }
            else//断开
            {
                AxleManager.Close();
                m_isConnected = false;
                imgConnection.Source = m_IconManager.DisConnected;
                //ChangeBitmapToBitmapSource( Properties.Resources.disconnected );

                //new BitmapImage(
                //new Uri(
                //    "/KZ600BApp;component/Images/disconnected.jpg"
                //    , UriKind.Relative
                //    )
                //);
                tbCxTip.Text = "联机";
            }
            System.Threading.Thread std = new System.Threading.Thread(new System.Threading.ThreadStart(enableBtnCnx));
            std.Start();
        }

        private void enableBtnCnx()
        {
            System.Threading.Thread.Sleep(3000);
            btnConnect.Dispatcher.Invoke(new Action(delegate()
            {
                btnConnect.IsEnabled = true;
            }));
        }

        private void btnUserDefine_Click(object sender, RoutedEventArgs e)
        {
            TPlan tplan = new TPlan();
            tplan.ShowDialog();
        }

        private void btnUserSetting_Click(object sender, RoutedEventArgs e)
        {
            UserSettings us = new UserSettings();
            us.ShowDialog();
        }

        bool m_bSimulating = false;
        private void btnSimulate_Click(object sender, RoutedEventArgs e)
        {
            ////测试pwm ratio
            //Axle t_Axle = AxleCacheManager.getAxle("X");
            //if (t_Axle != null)
            //{
            //   double pwm =  t_Axle.getRunPwm(50);
            //   Console.WriteLine(pwm);
            //}
            if (!m_bSimulating)
            {
                PositionCacheManager.SimulatorModify();
            }
            else
            {
                PositionCacheManager.SimulatorStop();
            }
            m_bSimulating = !m_bSimulating;
        }

        private void cmbTAngle_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbTAngle.SelectedItem != null && m_ATStatus != null)
            {
                int iValue = Convert.ToInt32(((ComboBoxItem)cmbTAngle.SelectedItem).Content);
                int lastAngle = m_ATStatus.CurrentAngle;
                int newAngle = iValue * 45;
                //proc

                setState(string.Format("T From {0} To {1}",lastAngle,newAngle));
                //if (newAngle == 0)
                //{
                //    moveT2Zero();
                //}
                //else
                //{
                    moveTAngle(true, newAngle);
                //}
                //new angle
                m_ATStatus.CurrentAngle = newAngle;
            }
        }

        private void setState(string msg)
        {
            KZ400B.Controls.StateMsgManager.addOneState(msg);
        }

        private void moveT2Zero()
        {
            string m_AxleName = "T";
            bool bRst = false;
            try
            {
                 bRst = AxleManager.ToZero(m_AxleName
                    , AxleCacheManager.AxleList[m_AxleName].getTozeroPwm(asT.TozeroSpeed)
                    );
                //AxleManager.setParameterChanged(AxleName, true);
            }
            catch (Exception ex)
            {
                setState(
                                string.Format("参数设置错误:({0})"
                                    , ex.Message
                                    )
                                );
            }
            finally
            {
                if (bRst)
                {
                    //setBtnState_AxleRunning();
                }
                //btnDelayEnable(sender); 
            }
        }

        private void moveTAngle(bool isForward,int angle)
        {
            bool bRst = false;
            int direct = Axle.Direction.ForwardDirection.Value;
            if (!isForward)
            {
                direct = Axle.Direction.BackDirection.Value;
            }
            string m_AxleName = "T";

            double totalStep = angle;
            setState(string.Format("total step = {0},directo = {1}",totalStep,isForward.ToString()));
            try
            {
                //if (totalStep == 0)
                //{
                //    return;
                //}
                Axle tAxle = AxleCacheManager.AxleList[m_AxleName];
                totalStep = AxleCacheManager.AxleList[m_AxleName].ComputeAddtion(totalStep, ref direct);

                if (totalStep == 0.0F)
                {
                    return;
                }
                AxleCacheManager.AxleList[m_AxleName].Addtion = totalStep;

                Axle.Direction dirt = new Axle.Direction(direct);
                double pwm = AxleCacheManager.AxleList[m_AxleName].getRunPwm(asT.RunSpeed);
                setState(string.Format("run pwm:{0}",pwm));
                AxleManager.Run(m_AxleName
                        , dirt
                        , pwm
                        , AxleCacheManager.AxleList[m_AxleName].TotalStep
                        , false
                        );
            }
            catch (Exception ex)
            {
                setState(
                                string.Format("参数设置错误:({0})"
                                    , ex.Message
                                    )
                                );
            }
            finally
            {
                if (bRst)
                {
                    //setBtnState_AxleRunning();
                }
            }
        }
    }
}
